package com.huang.A01_case;

import java.util.Random;
import java.util.Scanner;

/**
 * 目标
 * 抢红包
 */
public class Demo6 {
    public static void main(String[] args) {
        int[] moneys = {20, 30, 10, 999, 666};
        money1(moneys);
    }

    public static void money(int[] moneys) {
        //遍历数组
        Scanner sc = new Scanner(System.in);
        Random r = new Random();
        for (int i = 0; i < moneys.length; i++) {
            System.out.println("请输入随机内容进行抽奖~");
            sc.next();
            //进行抽奖
            //获得一个随机索引，如果不为0则中奖，然后将这个索引对应的
            //元素值改为0
            while (true) {
                int index = r.nextInt(moneys.length);
                if (moneys[index] != 0) {
                    System.out.println("恭喜你抽中了" + moneys[index] + "红包");
                    moneys[index] = 0;
                    break;
                }
            }
        }
    }

    public static void money1(int[] moneys) {
        //遍历数组 将数组中的红包进行打乱
        //将当前元素与随机索引位置的元素进行交换
        Scanner sc = new Scanner(System.in);
        Random r = new Random();
        for (int i = 0; i < moneys.length; i++) {
            int index = r.nextInt(moneys.length);
            //定义临时变量记录随机索引处的值
            int temp = moneys[index];
            //将当前元素的值赋值给随机索引对应的元素
            moneys[index] = moneys[i];
            //将temp里面存储的随机索引对应的元素值赋值给当前元素
            moneys[i] = temp;
        }

        //进行抽奖
        for (int i = 0; i < moneys.length; i++) {
            System.out.println("请输入随机内容进行抽奖~");
            sc.next();
            System.out.println("恭喜你抽中了" + moneys[i] + "红包");
        }
    }
}